package com.huangliusong.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.huangliusong.converter.OrderFormToOrderMaster;
import com.huangliusong.dataobject.OrderMaster;
import com.huangliusong.enums.ResultEnum;
import com.huangliusong.form.OrderForm;
import com.huangliusong.service.EasySendService;
import com.huangliusong.service.OrderService;
import com.huangliusong.service.impl.EasySendException;
import com.huangliusong.vo.ResultVO;
import com.huangliusong.vo.ResultVOUtil;

@RestController
@RequestMapping("/es/order")
public class EasySendOrderController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private EasySendService easySendService;
	@Autowired
	private RestTemplate restTemplate;
	// 创建订单 127.0.0.1:8080/es/order/create
	@SuppressWarnings("unchecked")
	@PostMapping("/create")
	@CrossOrigin
	public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			System.err.println("创建订单参数不正确" + orderForm);
			throw new EasySendException(ResultEnum.PARAM_ERROR.getCode(),
					bindingResult.getFieldError().getDefaultMessage());
		}
		OrderMaster orderMaster = OrderFormToOrderMaster.convert(orderForm);

		OrderMaster createResult = orderService.create(orderMaster);
		Map<String, String> map = new HashMap<>();
		map.put("orderId", createResult.getOrderId());
		return ResultVOUtil.success(map);
	}

	// 订单列表
	@SuppressWarnings("unchecked")
	@GetMapping("/list")
	public ResultVO<List<OrderMaster>> list(@RequestParam("openid") String openId,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size) {
		if (StringUtils.isEmpty(openId)) {
			System.err.println("openid为空");
			throw new EasySendException(ResultEnum.PARAM_ERROR);
		}
		PageRequest pageable = new PageRequest(page, size);
		Page<OrderMaster> ordreMasterResult = orderService.findList(openId, pageable);
		return ResultVOUtil.success(ordreMasterResult.getContent());
	}

	@SuppressWarnings("rawtypes")
	// 取消订单
	@PostMapping("/cancel")
	public ResultVO cancel(@RequestParam("openid") String openId, @RequestParam("orderid") String orderId) {
		easySendService.cancelOrder(openId, orderId);
		return ResultVOUtil.success();
	}
	
	@GetMapping("/location")
	@CrossOrigin
	public Object getLocation(@RequestParam("ip") String ip) {
		String url="http://api.map.baidu.com/location/ip?ak=HlC73DdZNi2UgVNdW7H0ciaatWUHgxPL&ip="+ip+"&coor=bd09ll";
		return  restTemplate.getForEntity(url, Object.class).getBody();
	
	}
}
